﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ServicePortScan
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        //主机地址
        private string hostAddress;
        //起始端口
        private int start;
        //终止端口
        private int end;
        //端口号
        private int port;
        //定义线程对象
        private Thread scanThread;
        //定义端口状态数据（开放则为true，否则为false）
        private bool[] done = new bool[65526];
        private bool OK;

        public Form1()
        {
            InitializeComponent();
            //不进行跨线程检查
            CheckForIllegalCrossThreadCalls = false;
            //simpleButton1.Location = new System.Drawing.Point(panel1.Location.X / 2, panel1.Location.Y / 2);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox1.Text = "";
                hostAddress = textBox1.Text.Trim();
                start = Int32.Parse(textBox2.Text);
                end = Int32.Parse(textBox3.Text);
                if (decideAddress()) // 端口合理
                {
                    Thread process = new Thread(new ThreadStart(PortScan));
                    process.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("输入错误，端口范围为[0-65536]!");
            }


        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.Focus();
            richTextBox1.Select(richTextBox1.Text.Length, 0);
            richTextBox1.ScrollToCaret();
        }

        /// <summary>
        /// 判断端口是否合理
        /// </summary>
        /// <returns></returns>
        private bool decideAddress()
        {
            //判断端口号是否合理
            if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end))
                return true;
            else
                return false;
        }
        private void PortScan()
        {
            double x;
            string xian;
            //显示扫描状态
            richTextBox1.Text = $"开始扫描...（可能需要请您等待几分钟）,当前时间：{DateTime.Now}";
            //循环抛出线程扫描端口
            for (int i = start; i <= end; i++)
            {
                x = (double)(i - start + 1) / (end - start + 1);
                xian = x.ToString("0%");
                port = i;
                //调用端口i的扫描操作
                Scan();
            }
            richTextBox1.Text += Environment.NewLine + "扫描结束！" + Environment.NewLine;
        }
        /// <summary>
        /// 扫描某个端口
        /// </summary>
        private void Scan()
        {
            int portnow = port;
            //创建TcpClient对象，TcpClient用于为TCP网络服务提供客户端连接
            TcpClient objTCP = null;
            try
            {
                //用于TcpClient对象扫描端口
                objTCP = new TcpClient(hostAddress, portnow);
                //扫描到则显示到显示框
                richTextBox1.Text += $"\r\n端口 {port}  开放！";
            }
            catch
            {
                //未扫描到，则会抛出错误
            }
        }
    }
}
